<?php
namespace Admin\Model;
use Think\Model;
class ShopModel extends Model 
{
	protected $insertFields = array('full_name','shop_name','type','bind_time','shop_addr','phone','seller_id','intro','is_del','status');
	protected $updateFields = array('shop_id','full_name','shop_name','type','bind_time','shop_addr','phone','seller_id','intro','is_del','status');
	protected $_validate = array(
//		array('full_name', 'require', '店铺全称不能为空！', 1, 'regex', 3),
//		array('full_name', '1,100', '店铺全称的值最长不能超过 100 个字符！', 1, 'length', 3),
//		array('shop_name', 'require', '店铺全程不能为空！', 1, 'regex', 3),
//		array('shop_name', '1,50', '店铺全程的值最长不能超过 50 个字符！', 1, 'length', 3),
//		array('type', 'require', '1品2住3餐4超5美6线7其他不能为空！', 1, 'regex', 3),
//		array('type', 'number', '1品2住3餐4超5美6线7其他必须是一个整数！', 1, 'regex', 3),
//
//		array('shop_addr', 'require', '店铺地址不能为空！', 1, 'regex', 3),
//		array('shop_addr', '1,100', '店铺地址的值最长不能超过 100 个字符！', 1, 'length', 3),
//		array('phone', 'number', '电话号码必须是一个整数！', 2, 'regex', 3),
////		array('seller_id', 'require', '商家的id不能为空！', 1, 'regex', 3),
////		array('seller_id', '1,32', '商家的id的值最长不能超过 32 个字符！', 1, 'length', 3),

	);
	public function search($pageSize = 20)
	{
		/**************************************** 搜索 ****************************************/
		$where = array();
		if($full_name = I('get.full_name'))
			$where['full_name'] = array('like', "%$full_name%");
		/************************************* 翻页 ****************************************/
		$count = $this->alias('a')->where($where)->count();
		$page = new \Think\Page($count, $pageSize);
		// 配置翻页的样式
		$page->setConfig('prev', '上一页');
		$page->setConfig('next', '下一页');
		$data['page'] = $page->show();
		/************************************** 取数据 ******************************************/
		$data['data'] = $this->alias('a')->where($where)->group('a.shop_id')->limit($page->firstRow.','.$page->listRows)->select();
		return $data;
	}
	// 添加前
	protected function _before_insert(&$data, $option)
	{
		if(isset($_FILES['license_img']) && $_FILES['license_img']['error'] == 0)
		{
			$ret = uploadOne('license_img', 'Admin', array(
				array(350, 350, 2),
			));
			if($ret['ok'] == 1)
			{
				$data['license_img'] = $ret['images'][0];
			}
			else 
			{
				$this->error = $ret['error'];
				return FALSE;
			}
		}
		if(isset($_FILES['permit_img']) && $_FILES['permit_img']['error'] == 0)
		{
			$ret = uploadOne('permit_img', 'Admin', array(
				array(350, 350, 2),
			));
			if($ret['ok'] == 1)
			{
				$data['permit_img'] = $ret['images'][0];
			}
			else 
			{
				$this->error = $ret['error'];
				return FALSE;
			}
		}
		if(isset($_FILES['ID_front_img']) && $_FILES['ID_front_img']['error'] == 0)
		{
			$ret = uploadOne('ID_front_img', 'Admin', array(
				array(350, 350, 2),
			));
			if($ret['ok'] == 1)
			{
				$data['ID_front_img'] = $ret['images'][0];
			}
			else 
			{
				$this->error = $ret['error'];
				return FALSE;
			}
		}
		if(isset($_FILES['ID_back_img']) && $_FILES['ID_back_img']['error'] == 0)
		{
			$ret = uploadOne('ID_back_img', 'Admin', array(
				array(350, 350, 2),
			));
			if($ret['ok'] == 1)
			{
				$data['ID_back_img'] = $ret['images'][0];
			}
			else 
			{
				$this->error = $ret['error'];
				return FALSE;
			}
		}
		if(isset($_FILES['shop_logo_img']) && $_FILES['shop_logo_img']['error'] == 0)
		{
			$ret = uploadOne('shop_logo_img', 'Admin', array(
				array(350, 350, 2),
			));
			if($ret['ok'] == 1)
			{
				$data['shop_logo_img'] = $ret['images'][0];
			}
			else 
			{
				$this->error = $ret['error'];
				return FALSE;
			}
		}
		if(isset($_FILES['shop_img1']) && $_FILES['shop_img1']['error'] == 0)
		{
			$ret = uploadOne('shop_img1', 'Admin', array(
				array(350, 350, 2),
			));
			if($ret['ok'] == 1)
			{
				$data['shop_img1'] = $ret['images'][0];
			}
			else 
			{
				$this->error = $ret['error'];
				return FALSE;
			}
		}
		if(isset($_FILES['shop_img2']) && $_FILES['shop_img2']['error'] == 0)
		{
			$ret = uploadOne('shop_img2', 'Admin', array(
				array(350, 350, 2),
			));
			if($ret['ok'] == 1)
			{
				$data['shop_img2'] = $ret['images'][0];
			}
			else 
			{
				$this->error = $ret['error'];
				return FALSE;
			}
		}
		if(isset($_FILES['Front1_img']) && $_FILES['Front1_img']['error'] == 0)
		{
			$ret = uploadOne('Front1_img', 'Admin', array(
				array(350, 350, 2),
			));
			if($ret['ok'] == 1)
			{
				$data['Front1_img'] = $ret['images'][0];
			}
			else 
			{
				$this->error = $ret['error'];
				return FALSE;
			}
		}
		if(isset($_FILES['front2_img']) && $_FILES['front2_img']['error'] == 0)
		{
			$ret = uploadOne('front2_img', 'Admin', array(
				array(350, 350, 2),
			));
			if($ret['ok'] == 1)
			{
				$data['front2_img'] = $ret['images'][0];
			}
			else 
			{
				$this->error = $ret['error'];
				return FALSE;
			}
		}
	}
	// 修改前
	protected function _before_update(&$data, $option)
	{
		if(isset($_FILES['license_img']) && $_FILES['license_img']['error'] == 0)
		{
			$ret = uploadOne('license_img', 'Admin', array(
				array(350, 350, 2),
			));
			if($ret['ok'] == 1)
			{
				$data['license_img'] = $ret['images'][0];
			}
			else 
			{
				$this->error = $ret['error'];
				return FALSE;
			}
			deleteImage(array(
				I('post.old_license_img'),
	
			));
		}
		if(isset($_FILES['permit_img']) && $_FILES['permit_img']['error'] == 0)
		{
			$ret = uploadOne('permit_img', 'Admin', array(
				array(350, 350, 2),
			));
			if($ret['ok'] == 1)
			{
				$data['permit_img'] = $ret['images'][0];
			}
			else 
			{
				$this->error = $ret['error'];
				return FALSE;
			}
			deleteImage(array(
				I('post.old_permit_img'),
	
			));
		}
		if(isset($_FILES['ID_front_img']) && $_FILES['ID_front_img']['error'] == 0)
		{
			$ret = uploadOne('ID_front_img', 'Admin', array(
				array(350, 350, 2),
			));
			if($ret['ok'] == 1)
			{
				$data['ID_front_img'] = $ret['images'][0];
			}
			else 
			{
				$this->error = $ret['error'];
				return FALSE;
			}
			deleteImage(array(
				I('post.old_ID_front_img'),
	
			));
		}
		if(isset($_FILES['ID_back_img']) && $_FILES['ID_back_img']['error'] == 0)
		{
			$ret = uploadOne('ID_back_img', 'Admin', array(
				array(350, 350, 2),
			));
			if($ret['ok'] == 1)
			{
				$data['ID_back_img'] = $ret['images'][0];
			}
			else 
			{
				$this->error = $ret['error'];
				return FALSE;
			}
			deleteImage(array(
				I('post.old_ID_back_img'),
	
			));
		}
		if(isset($_FILES['shop_logo_img']) && $_FILES['shop_logo_img']['error'] == 0)
		{
			$ret = uploadOne('shop_logo_img', 'Admin', array(
				array(350, 350, 2),
			));
			if($ret['ok'] == 1)
			{
				$data['shop_logo_img'] = $ret['images'][0];
			}
			else 
			{
				$this->error = $ret['error'];
				return FALSE;
			}
			deleteImage(array(
				I('post.old_shop_logo_img'),
	
			));
		}
		if(isset($_FILES['shop_img1']) && $_FILES['shop_img1']['error'] == 0)
		{
			$ret = uploadOne('shop_img1', 'Admin', array(
				array(350, 350, 2),
			));
			if($ret['ok'] == 1)
			{
				$data['shop_img1'] = $ret['images'][0];
			}
			else 
			{
				$this->error = $ret['error'];
				return FALSE;
			}
			deleteImage(array(
				I('post.old_shop_img1'),
	
			));
		}
		if(isset($_FILES['shop_img2']) && $_FILES['shop_img2']['error'] == 0)
		{
			$ret = uploadOne('shop_img2', 'Admin', array(
				array(350, 350, 2),
			));
			if($ret['ok'] == 1)
			{
				$data['shop_img2'] = $ret['images'][0];
			}
			else 
			{
				$this->error = $ret['error'];
				return FALSE;
			}
			deleteImage(array(
				I('post.old_shop_img2'),
	
			));
		}
		if(isset($_FILES['Front1_img']) && $_FILES['Front1_img']['error'] == 0)
		{
			$ret = uploadOne('Front1_img', 'Admin', array(
				array(350, 350, 2),
			));
			if($ret['ok'] == 1)
			{
				$data['Front1_img'] = $ret['images'][0];
			}
			else 
			{
				$this->error = $ret['error'];
				return FALSE;
			}
			deleteImage(array(
				I('post.old_Front1_img'),
	
			));
		}
		if(isset($_FILES['front2_img']) && $_FILES['front2_img']['error'] == 0)
		{
			$ret = uploadOne('front2_img', 'Admin', array(
				array(350, 350, 2),
			));
			if($ret['ok'] == 1)
			{
				$data['front2_img'] = $ret['images'][0];
			}
			else 
			{
				$this->error = $ret['error'];
				return FALSE;
			}
			deleteImage(array(
				I('post.old_front2_img'),
	
			));
		}
	}
	// 删除前
	protected function _before_delete($option)
	{
		if(is_array($option['where']['shop_id']))
		{
			$this->error = '不支持批量删除';
			return FALSE;
		}
		$images = $this->field('license_img')->find($option['where']['shop_id']);
		deleteImage($images);
		$images = $this->field('permit_img')->find($option['where']['shop_id']);
		deleteImage($images);
		$images = $this->field('ID_front_img')->find($option['where']['shop_id']);
		deleteImage($images);
		$images = $this->field('ID_back_img')->find($option['where']['shop_id']);
		deleteImage($images);
		$images = $this->field('shop_logo_img')->find($option['where']['shop_id']);
		deleteImage($images);
		$images = $this->field('shop_img1')->find($option['where']['shop_id']);
		deleteImage($images);
		$images = $this->field('shop_img2')->find($option['where']['shop_id']);
		deleteImage($images);
		$images = $this->field('Front1_img')->find($option['where']['shop_id']);
		deleteImage($images);
		$images = $this->field('front2_img')->find($option['where']['shop_id']);
		deleteImage($images);
	}
	/************************************ 其他方法 ********************************************/
}